﻿using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Work.Hardware.ETS;

namespace FreesenseSubAIS.Core
{
    public class ETSDemo
    {
        private ILogger _log;
        public bool Init()
        {
            _log = LogManager.GetCurrentClassLogger();
            bool result = false;
            string configName = string.Format("ETSConfig_{0}.txt", this.Name);
            if (File.Exists(configName))
            {
                using (FileStream fs = new FileStream(configName, FileMode.Open, FileAccess.Read))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        ETSIP = sr.ReadLine().Trim();
                        ETSPort = sr.ReadLine().Trim();
                        ETSProduct = sr.ReadLine().Trim();
                        ETSImagesCount = Convert.ToInt32(sr.ReadLine().Trim());
                        ETSImageSleepTime = Convert.ToInt32(sr.ReadLine().Trim());
                        ETSReceiveCommandTimeout = Convert.ToInt32(sr.ReadLine().Trim());
                        ETSWaitLoadProductTimeout = Convert.ToInt32(sr.ReadLine().Trim());
                    }
                }
                ETSInstance = new ETS
                {
                    receiveCommandTimeout = ETSReceiveCommandTimeout,
                    WaitLoadProductTimeout = ETSWaitLoadProductTimeout
                };
                bool connectResult = ETSInstance.Connect(ETSIP, ETSPort);
                if (connectResult)
                {
                    Task.Factory.StartNew(() =>
                        {
                            int loadResult = ETSInstance.LoadProduct(ETSProduct);
                            if (loadResult == 1)
                            {
                                if (OnMessageCallback != null)
                                {
                                    OnMessageCallback(this, "ETS加载产品成功");
                                }
                            }
                            else
                            {
                                if (OnMessageCallback != null)
                                {
                                    OnMessageCallback(this, "ETS加载产品失败");
                                }
                            }
                        }, TaskCreationOptions.LongRunning);
                    result = true;
                }
                else
                {
                    if (OnMessageCallback != null)
                    {
                        OnMessageCallback(this, "ETS初始化失败");
                    }
                    result = false;
                }
            }
            else
            {
                if (OnMessageCallback != null)
                {
                    OnMessageCallback(this, "ETS配置文件不存在");
                }

                result = false;
            }
            return result;
        }

        public void SwitchImages(int index)
        {
            if (OnMessageCallback != null)
            {
                OnMessageCallback(this, "ETS尝试切图"+index);
            }
            if (ETSInstance == null) return;
            var switchResult = ETSInstance.SwitchImage(index);
            if (switchResult != 1)
            {
                _log.Info(string.Format ("点灯机切图失败，返回结果{0}",switchResult ));
                ShutDown();
                _log.Info("点灯机断电成功");
            }
        }
        public void ShutDown()
        {
            if (OnMessageCallback != null)
            {
                OnMessageCallback(this, "ETS尝试熄屏");
            }
            ETSInstance.ShutDownScreen();
        }
        public bool PowerON()
        {
            if (OnMessageCallback != null)
            {
                OnMessageCallback(this, "ETS尝试点屏");
            }
            var lightUpResult = ETSInstance.LightUpScreen();
            if (lightUpResult == 1)
            {
                return true;
            }
            else
            {
                _log.Info(string.Format("点灯机上电失败，返回结果{0}", lightUpResult));
                ShutDown();
                _log.Info("点灯机断电成功");
                return false;
            }
        }
        public void Close()
        {
            if (ETSInstance != null)
            {
                ETSInstance.Close();
            }
        }

        /// <summary>
        /// ETSDemo的实例名称，同时区分配置文件
        /// </summary>
        public string Name = string.Empty;

        /// <summary>
        /// ETS的IP地址
        /// </summary>
        public string ETSIP = string.Empty;

        /// <summary>
        /// ETS端口号
        /// </summary>
        public string ETSPort = string.Empty;

        /// <summary>
        /// 加载的产品名称
        /// </summary>
        public string ETSProduct = string.Empty;

        /// <summary>
        /// 图片总数
        /// </summary>
        public int ETSImagesCount = 0;

        /// <summary>
        /// 切图间隔时间
        /// </summary>
        public int ETSImageSleepTime = 500;

        /// <summary>
        /// 普通命令等待超时时间
        /// </summary>
        public int ETSReceiveCommandTimeout = 1000;

        /// <summary>
        /// 加载产品等待超时时间
        /// </summary>
        public int ETSWaitLoadProductTimeout = 120000;

        public ETS ETSInstance = null;

        public event EventHandler<string> OnMessageCallback = null;
    }
}
